# -*- coding: utf-8 -*-
"""
@Author : Chan ZiWen
@Date : 2022/5/31 15:27
File Description:

"""
import matplotlib.pyplot as plt


def show(p, date, sc_mac, activeId, y=None, axvline: tuple = (None, None), ifsave=None):
	"""
	:param p: pandas.dataframe
	:param axvline: vertical line
	"""
	assert y.shape
	p.plot()
	plt.title(f'{date} | MAC {sc_mac} | Activation ID {activeId}')
	plt.ylabel('Scan the distance from the TV to the mobile device(Meter)')
	if y is not None:
		plt.plot(y, c='red', linewidth=2)
	if axvline[0] is not None:
		plt.axvline(axvline[0], c='black', linewidth=4)
		plt.text(x=axvline[0]+2, y=38, s=f'{axvline[1]}')
	if ifsave:
		plt.savefig(ifsave, format='png', bbox_inches="tight")
	plt.close()


def steady(df: list = None):
	"""
	exponentially weighted averages	 (beta = 0.9)
	math equal:
		y_t = \beta * x_{t-1} + (1 - /beta) * x_t

	we can compute average how days :
		there's exponentially weighted average, just adapts more slowly when beta is so large.
	随着着前值大小将当前值
	:return:
	"""
	n = len(df)
	df_new = df.copy()
	fraction = 40

	for i in range(1, n):
		diff = df[i] - df[i-1]
		fraction_ratio = (1 - diff / fraction) * 0.5
		df_new[i] = df[i] - diff * fraction_ratio
		# print(f"{diff: .4f} - {df[i]: .4f}, {diff * fraction_ratio: .4f} - {df_new[i]: .4f}  ")
	return df_new
